package com.mcxx.modules.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mcxx.common.exception.BizException;
import com.mcxx.convert.StatisticsConvert;
import com.mcxx.modules.system.service.StatisticsExportService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.external.guicai.service.write.*;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayReadService;
import com.mcxx.modules.statistics.entity.StatisticsExportEntity;
import com.mcxx.modules.system.dto.StatisticsExportDTO;
import com.mcxx.modules.system.service.read.StatisticsExportItemReadService;
import com.mcxx.modules.system.service.read.StatisticsExportReadService;
import com.mcxx.modules.system.service.write.StatisticsExportWriteService;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.OkHttpUtil;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@Service
@TaTransactional
public class StatisticsExportServiceImpl implements StatisticsExportService {

    private final static Logger logger = LoggerFactory.getLogger(StatisticsExportServiceImpl.class);
    @Autowired
    private StatisticsExportWriteService statisticsExportWriteService;
    @Autowired
    private StatisticsExportReadService statisticsExportReadService;
    @Autowired
    private FundPayReadService fundPayReadService;
    @Autowired
    private StatisticsExportItemReadService statisticsExportItemReadService;
    @Autowired
    private SHJZT204Y19WriteService shJZT204Y19WriteService;
    @Autowired
    private SHJZT205Y19WriteService shJZT205Y19WriteService;
    @Autowired
    private SHJZT206Y19WriteService shJZT206Y19WriteService;
    @Autowired
    private SHJZT207Y19WriteService shJZT207Y19WriteService;
    @Autowired
    private SHJZT225Y19WriteService shJZT225Y19WriteService;

    @Value("${static.url}")
    private String staticUrl;


    @Override
    public void createStatistics(StatisticsExportDTO statisticsExportDTO, UserAccountVo userAccountVo){

        this.setValueByObjectType(statisticsExportDTO);

        QueryParamUtil.setBaseParamValue(statisticsExportDTO, userAccountVo);


        fundPayReadService.verifyConfirmFundPayThrow(statisticsExportDTO.getBussinessType(), userAccountVo.getAreaCode(),
                statisticsExportDTO.getStandType(), statisticsExportDTO.getYears(),null,
                Constant.FundType.CAPITAL_FUND_PAY,Constant.FundPayState.CONFIRM);

        String statisId = "";
        StatisticsExportEntity entity =  statisticsExportReadService.selectRepeatByAreaCode(userAccountVo.getAreaCode(),statisticsExportDTO.getObjectType(),statisticsExportDTO.getYears());
        if(CommonUtil.invalidArgs(entity)){

            statisticsExportDTO.setId(Constant.getUUID());
            StatisticsExportEntity statisticsExportEntity = StatisticsConvert.convertStatisticsExportEntity(statisticsExportDTO, userAccountVo);
            statisticsExportEntity.setStatus(Constant.StatisticsStatus.CREAT_EXPORT_ING);
            statisticsExportEntity.setAreaCode(userAccountVo.getAreaCode());
            statisticsExportWriteService.insertIgnoreNull(statisticsExportEntity);
            statisId = statisticsExportDTO.getId();
        }else{
            if(Constant.StatisticsStatus.CREAT_EXPORT_ING.equals(entity.getStatus())){
                throw new BizException("-1","台账生成中，请勿重复生成。");
            }

            entity.setStatus(Constant.StatisticsStatus.CREAT_EXPORT_ING);
            entity.setFilename("");
            statisticsExportWriteService.updateIgnoreNull(entity);
            statisId = entity.getId();
        }

        String guicaiType = "";
        if(Constant.BusinessCode.DIBAO.equals(statisticsExportDTO.getBussinessType()) && Constant.BusinessStandType.CITY_DIBAO.equals(statisticsExportDTO.getStandType())){
            guicaiType = Constant.TaizhangType.CITY_DIBAO;
        }else if(Constant.BusinessCode.DIBAO.equals(statisticsExportDTO.getBussinessType()) &&  Constant.BusinessStandType.COUNTY_DIBAO.equals(statisticsExportDTO.getStandType())){
            guicaiType = Constant.TaizhangType.COUNTRY_DIBAO;
        }else if(Constant.BusinessCode.TEKUN.equals(statisticsExportDTO.getBussinessType()) && Constant.BusinessStandType.CITY_TEKUN.equals(statisticsExportDTO.getStandType())){
            guicaiType = Constant.TaizhangType.CITY_TEKUN;
        }else if(Constant.BusinessCode.TEKUN.equals(statisticsExportDTO.getBussinessType()) && Constant.BusinessStandType.COUNTY_TEKUN.equals(statisticsExportDTO.getStandType())){
            guicaiType = Constant.TaizhangType.COUNTRY_TEKUN;
        }else if(Constant.BusinessCode.LINSHI.equals(statisticsExportDTO.getBussinessType())){
            guicaiType = Constant.TaizhangType.LINSHI;
        }

        String url = staticUrl;
        JSONObject json = new JSONObject();

        url += "?id=" + statisId;
        url += "&areaCodes=" + userAccountVo.getAreaCode().substring(0,6);
        url += "&months=" + statisticsExportDTO.getYears().substring(0,4)+statisticsExportDTO.getYears().substring(5,7);
        url += "&guicaiType=" + guicaiType;
        String res = OkHttpUtil.doPost(url,json.toString(),null);
        logger.info("创建台账结束，响应结果:{}", res);
    }


    @Override
    public void confirmStatistics(HttpServletResponse response, String statisticId, String objectType) {
        List<String> statisticIdList = new ArrayList<String>(){{add(statisticId);}};

        statisticsExportReadService.verifyStatus(statisticIdList, new ArrayList<String>(){{add(Constant.StatisticsStatus.FINISH);}});


        statisticsExportWriteService.updateStatusByIds(statisticIdList, Constant.StatisticsStatus.CONFIRM);


        statisticsExportItemReadService.generateExportTxt(response, statisticId, objectType);
    }

    @Override
    public void deleteStatistics(List<String> statisticIdList) {

        statisticsExportReadService.verifyStatus(statisticIdList, new ArrayList<String>(){{add(Constant.StatisticsStatus.CREAT_EXPORT_ING);
            add(Constant.StatisticsStatus.FINISH);add(Constant.StatisticsStatus.ERROR);}});


        //statisticsExportWriteService.updateStatusByIds(statisticIdList, Constant.StatisticsStatus.DELETE);

        for (String staticId : statisticIdList){
            StatisticsExportEntity en = statisticsExportReadService.getById(staticId);
            statisticsExportWriteService.delete(en);

            if(Constant.GuicaiType.CITY_DIBAO.equals(en.getObjectType())){

                shJZT204Y19WriteService.delete(staticId);
            }else if(Constant.GuicaiType.COUNTY_DIBAO.equals(en.getObjectType())){

                shJZT205Y19WriteService.delete(staticId);
            }else if(Constant.GuicaiType.CITY_TEKUN.equals(en.getObjectType())){

                shJZT206Y19WriteService.delete(staticId);
            }else if(Constant.GuicaiType.COUNTY_TEKUN.equals(en.getObjectType())){

                shJZT207Y19WriteService.delete(staticId);
            }else{

                shJZT225Y19WriteService.delete(staticId);
            }
        }
    }



    private void setValueByObjectType(StatisticsExportDTO statisticsExportDTO){
        if(Constant.GuicaiType.CITY_DIBAO.equals(statisticsExportDTO.getObjectType())){

            statisticsExportDTO.setBussinessType(Constant.BusinessCode.DIBAO);
            statisticsExportDTO.setStandType(Constant.BusinessStandType.CITY_DIBAO);
        }else if(Constant.GuicaiType.COUNTY_DIBAO.equals(statisticsExportDTO.getObjectType())){

            statisticsExportDTO.setBussinessType(Constant.BusinessCode.DIBAO);
            statisticsExportDTO.setStandType(Constant.BusinessStandType.COUNTY_DIBAO);
        }else if(Constant.GuicaiType.CITY_TEKUN.equals(statisticsExportDTO.getObjectType())){

            statisticsExportDTO.setBussinessType(Constant.BusinessCode.TEKUN);
            statisticsExportDTO.setStandType(Constant.BusinessStandType.CITY_TEKUN);
        }else if(Constant.GuicaiType.COUNTY_TEKUN.equals(statisticsExportDTO.getObjectType())){

            statisticsExportDTO.setBussinessType(Constant.BusinessCode.TEKUN);
            statisticsExportDTO.setStandType(Constant.BusinessStandType.COUNTY_TEKUN);
        }else{

            statisticsExportDTO.setBussinessType(Constant.BusinessCode.LINSHI);
        }
    }
}
